package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.DictionaryDetail;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.domain.Dictionary;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionary")
public class DictionaryController {
    @Autowired
    private IDictionaryService dictionaryService;

    @RequestMapping("selectDetailsByDicId")
    @ResponseBody
    public List<DictionaryDetail> selectDetailsByDicId(Long id){
        List<DictionaryDetail> dictionaryDetails = dictionaryService.selectDetailsByDicId(id);
        return dictionaryDetails;
    }

    @RequestMapping("")
    @RequiresPermissions(":index")
    @PermissionName("数据字典列表")
    public String index() {
        //只负责跳转到dictionary.jsp页面即可
        return "dictionary";
    }

    @RequestMapping("list")
    @RequiresPermissions("dictionary:list")
    @PermissionName("数据字典数据")
    @ResponseBody
    public PageResult list(QueryObject qo) {
        PageResult result = dictionaryService.query(qo);
        return result;
    }

    @RequestMapping("delete")
    @RequiresPermissions("dictionary:delete")
    @PermissionName("数据字典删除")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            dictionaryService.deleteByPrimaryKey(id);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions("dictionary:saveOrUpdate")
    @PermissionName("数据字典工保存")
    @ResponseBody
    public JsonResult saveOrUpdate(Dictionary dictionary) {
        JsonResult result = new JsonResult();
        try {
            if (dictionary.getId() == null) {
                dictionaryService.insert(dictionary);
                result.setMessage("保存成功");
            } else {
                dictionaryService.updateByPrimaryKey(dictionary);
                result.setMessage("编辑成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
}
